package com.mm.controller.system;


import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mm.entity.MmMenu;
import com.mm.exception.BusinessException;
import com.mm.service.impl.MmMenuServiceImpl;
import com.mm.enums.ResultCodeEnum;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wdx
 * @since 2020-09-16
 */
@Api(tags = "菜单API")
@Validated
@RestController
@RequestMapping("/mm-menu")
public class MmMenuController {

    @Autowired
    MmMenuServiceImpl mmMenuService;


    @ApiOperation(value = "树形菜单列表")
    @GetMapping
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pid", value = "父级ID", dataType = "int", paramType = "query")
    })
    public ApiResult<MmMenu> list(Integer pid) {
        List<MmMenu> all = mmMenuService.list(query().eq(MmMenu::getMenuType, 0).isNotNull(MmMenu::getPid));
        List<MmMenu> list = mmMenuService.list(query().eq(MmMenu::getMenuType, 0).isNull(pid == null, MmMenu::getPid).eq(pid != null, MmMenu::getPid, pid));
        //一级菜单有子节点 赋值 true
        if (pid == null) {
            for (MmMenu mmMenu : list) {
                if (all.stream().map(w -> w.getPid() == mmMenu.getId()).count() > 0){
                    mmMenu.setHasChildren(true);
                }
            }
        }
        return ApiResult.success(list);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public ApiResult save(@RequestBody MmMenu mMenu) {
        boolean save = mmMenuService.save(mMenu);
        if (!save) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<MmMenu> detail(@NotNull(message = "ID不能为空")@PathVariable Integer id) {
        MmMenu mMenu = mmMenuService.getById(id);
        if (mMenu == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
        return ApiResult.success(mMenu);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public ApiResult update(@RequestBody MmMenu mMenu) {
        boolean update = mmMenuService.updateById(mMenu);
        if (!update) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @DeleteMapping("/{id}")
    public ApiResult del(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        boolean del = mmMenuService.removeById(id);
        if (!del) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    /**
     * 查询条件简化
     *
     * @return
     */
    public LambdaQueryWrapper<MmMenu> query() {
        return new LambdaQueryWrapper<MmMenu>();
    }


}
